<!DOCTYPE HTML>

<?php 
	session_start(); 

	//Conexión a la bd
	$link = mysqli_connect('localhost', 'root', '') or die("Failed to connect to MySQL: " . mysql_error());
	if (mysqli_connect_errno()) {
	        echo 'Failed to connect to MySQL';
	        exit;
	}
	$sql = "USE web_db";
	$query = mysqli_query($link,$sql);
	
	if(isset($_SESSION['id'], $_SESSION['vorname'], $_SESSION['name'])){
		$id = $_SESSION['id'];
		$vorname = $_SESSION['vorname'];
		$name = $_SESSION['name'];
	}


	function load_pilots(){
		global $link;

		$sql = "SELECT vorname, name, id
				FROM staff st
				WHERE id <> 'root'";
		$query = mysqli_query($link,$sql);


		while($row = mysqli_fetch_row($query)){
			echo "<option value=\"" . $row[2] . "\">" . $row[2] . " - " . $row[1] . ", " . $row[0] . "</option>";
		}
	}

	function load_parts($from){
		global $link;

		$sql = "SELECT sn, name
				FROM part
				WHERE kind = '$from'";
		$query = mysqli_query($link,$sql);


		while($row = mysqli_fetch_row($query)){
			echo "<option value=\"" . $row[0] . "\">" . $row[0] . " - " . $row[1] . "</option>";
		}
	}
?>
<html>

<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<link rel="stylesheet" type="text/css" href="./css/index.css">
<link href='./images/favicon.ico' rel='shortcut icon' type='image/png'>
<title>Mecha</title>
</head>


<body id="fondo">
<div id="cuerpo">
<fieldset style="width: 1000px; color: #FFF;";>
    <?php
        include 'header.php';
    ?>
<div id="subcuerpo">
	<table>
		<form method="POST" action="new_mecha.php">
			<tr>
				<td id="principal" colspan='2'><h2>New mecha unit</h2></td>
			</tr>
			<tr>
				<td style="text-align:right; width=150px">ID: </td><td><input type="text" id="mec_id" name="mec_id"></td>
			</tr>
			<tr>
				<td width='150px' style="text-align:right">Asign to: </td><td><select id="pilot_id" name="pilot_id"> <?php load_pilots();?></select></td>
			</tr>
			<tr><td></td><td>
			<table style="text-align:center">	
				<tr>
					<td></td><td>Head:<br><select id="head" name="head"> <?php load_parts('Head');?></select></td>
				</tr>
				<tr>
					<td></td><td>Chest:<br><select id="chest" name="chest"> <?php load_parts('Chest');?></select></td>
				</tr>
				<tr>
					<td>Weapon 1:<br><select id="weapon1" name="weapon1"> <?php load_parts('Weapon');?></select></td>
					<td>Arms:<br><select id="arms" name="arms"> <?php load_parts('Arms');?></select></td>
					<td>Weapon 2:<br><select id="weapon2" name="weapon2"> <?php load_parts('Weapon');?></select></td>
				</tr>
				<tr>
					<td></td><td>Legs:<br><select id="legs" name="legs"> <?php load_parts('Legs');?></select></td>
				</tr>
				<tr>
					<td></td><td>Other:<br><select id="other" name="other"> <?php load_parts('Others');?></select></td>
				</tr>
			</table>
			</td></tr>

			<tr>
				<td></td><td style="text-align:left"><input id="button" type="submit" name="submit" value="Confirm"></td>
			</tr>
			</form>
	</table>
</div>
<?php
	include 'footer.php';
?>
</fieldset>
</div>